'use strict';
angular.module('Eamclient.filters', ['config'])

/**
 * A simple relative timestamp filter
 * http://codepen.io/Samurais/pen/PwwLPK
 * https://gist.github.com/Samurais/0c9e81eb18c3d60db46c
 */
.filter('relativets', function($log) {

    // ms units
    var second = 1000;
    var minute = 60000;
    var hour = 3600000;
    var day = 86400000;
    var year = 31536000000;
    var month = 2592000000;

    function _formatDateString(val) {
        var date = new Date(val);
        var yyyy = date.getFullYear();
        var mm = date.getMonth() + 1; //January is 0!
        var dd = date.getDate();
        var hh = date.getHours();
        var min = date.getMinutes();
        var sec = date.getSeconds();

        if (mm < 10) {
            mm = '0' + mm;
        }
        if (dd < 10) {
            dd = '0' + dd;
        }
        if (hh < 10) {
            hh = '0' + hh;
        }
        if (min < 10) {
            min = '0' + min;
        }
        if (sec < 10) {
            sec = '0' + sec;
        }

        return '{0}/{1}/{2} {3}:{4}'.format(yyyy, mm, dd, hh, min);
    };

    return function(value) {
        var diff = new Date() - new Date(value);
        var unit = day;
        var unitStr = t('filter_minutes_ago');
        if (diff > year || diff > month || diff > day || diff > 3600000 * 2) {
            // big gap, just return the absolute time
            return _formatDateString(value);
        } else if (diff > hour) {
            unit = hour;
            unitStr = t('filter_hours_ago');
        } else if (diff > minute) {
            unit = minute;
            unitStr = t('filter_minutes_ago');
        } else {
            unit = second;
            unitStr = t('filter_seconds_ago');
        }

        var amt = Math.ceil(diff / unit);
        return amt + ' ' + unitStr;
    };
})

.filter('i18ng', function() {

    return function(val) {
        return t(val);
    }
})

.filter('wostatus', function() {
    // 0 : created inspection wo
    // 1 : assigned inspection wo
    // 2 : closed inspection wo
    // 3 : pending maintenance wo
    // 4 : assigned maintenance wo
    // 5 : closed maintenance wo
    // 6 : report
    var status = [t('wo_status_insp_pending'), t('wo_status_insp_assigned'), t('wo_status_insp_closed'),
        t('wo_status_main_pending'), t('wo_status_main_assigned'), t('wo_status_main_closed'), t('wo_status_report')
    ]

    return function(index) {
        return status[index];
    }
})

;
